package cn.youyeku.zsxh.ui.activity.tabAct;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.youyeku.zsxh.R;
import cn.youyeku.zsxh.Utils.LogUtils;
import cn.youyeku.zsxh.Utils.NullUtils;

import cn.youyeku.zsxh.api.XhApi;
import cn.youyeku.zsxh.app;
import cn.youyeku.zsxh.ui.activity.ABS.FeedbackActivity;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


public class ActGoodsExchangePost extends FeedbackActivity implements View.OnClickListener {

    @BindView(R.id.ed_send_help_message_title)
    EditText edSendHelpMessageTitle;
    @BindView(R.id.ed_send_help_message_price)
    EditText edSendHelpMessagePrice;
    @BindView(R.id.ed_send_help_message_text)
    EditText edSendHelpMessageText;
    @BindView(R.id.btn_upload)
    Button btnUpload;
    @BindView(R.id.iv_send_help_message_img)
    ImageView ivSendHelpMessageImg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_act_goods_exchange_send);
        ButterKnife.bind(this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        menu.add(Menu.NONE, Menu.FIRST, Menu.NONE, "发布")
                .setEnabled(true)
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);


        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case Menu.FIRST:
                postData();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void postData() {


        String title = edSendHelpMessageTitle.getText().toString();
        String content = edSendHelpMessageText.getText().toString();
        LogUtils.w(title+content);


        if (NullUtils.isArrayNotNull(new String[]{ title, content, local_file_path})) {
            XhApi.post_shop(title, content, local_file_path, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {

                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    try {
                        JSONObject object = new JSONObject(response.body().string());
                            app.backgroundThreadShortToast(object.getString("message"));
                            if(object.getInt("code")==200){
                                finish();
                            }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            });

        }


    }

    @Override
    protected void setPicturePreview(Uri resultUri) {
        Glide.with(getApplicationContext())
                .load(resultUri)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .error(R.drawable.default_avatar)
                .into(ivSendHelpMessageImg);
    }



    @OnClick({R.id.btn_upload})
    @Override
    public void onClick(View v) {

        getResources().getResourceEntryName(v.getId());

        switch (v.getId()) {
            case R.id.btn_upload:
                String[] array = {"图库", "拍照"};
                new MaterialDialog.Builder(this)
                        .title("选择")
                        .items(array)
                        .itemsCallback(new MaterialDialog.ListCallback() {
                            @Override
                            public void onSelection(MaterialDialog dialog, View itemView, int position, CharSequence text) {
//                                Log.d("position是：", "" + position);
                                if (position == 0) {//图库
                                    pickFromGallery();

                                } else if (position == 1) {//相机
                                    pickFromCamera();
                                }
                            }
                        }).show();
                break;
        }


    }

    public void pickFromGallery() {

        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(intent, GALLERY_REQUEST_CODE);
    }

    public void pickFromCamera() {

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(getExternalCacheDir(), IMAGE_FILE_NAME)));
        startActivityForResult(intent, CAMERA_REQUEST_CODE);
    }

}
